<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.bizTypeName" size="small" placeholder="业务类型代码/名称" style="width: 200px;" class="filter-item" />
      <el-button size="small" type="primary" round @click="handleFilter">查询</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="业务类型代码" align="center" width="110">
          <template slot-scope="{row}">
            <span>{{ row.bizTypeCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="业务类型名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.bizTypeName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编码总长度" align="center">
          <template slot-scope="{row}">
            <span>{{ row.len }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编码日期类型" align="center">
          <template slot-scope="{row}">
            <span>{{ row.dateType == 1 ? '年月日' : row.dateType == 2 ? '年月' : row.dateType ===3 ? '年' : '无日期' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编码前缀" align="center">
          <template slot-scope="{row}">
            <span>{{ row.preCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="{row}">
            <span>{{ row.remarks }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" align="center">
          <template slot-scope="{row}">
            <span>{{ row.isDisable==0?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="{row}">
            <el-button type="text" size="small" @click="handleCompile(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增业务类型':'修改业务类型'" :visible.sync="dialogVisible" width="500px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="110px" style="width: 456px;">
        <el-form-item label="业务类型代码" prop="bizTypeCode">
          <el-input v-model="form.bizTypeCode" disabled placeholder="业务类型代码" />
        </el-form-item>
        <el-form-item label="业务类型名称" prop="bizTypeName">
          <el-input v-model="form.bizTypeName" disabled placeholder="业务类型名称" />
        </el-form-item>
        <el-form-item label="编码总长度" prop="len">
          <el-input-number v-model="form.len" :min="6" :max="16" placeholder="编码总长度" />
        </el-form-item>
        <el-form-item label="编码日期类型" prop="dateType">
          <el-select v-model="form.dateType" style="width:100%" class="filter-item">
            <el-option label="无日期" :value="0" />
            <el-option label="年月日" :value="1" />
            <el-option label="年月" :value="2" />
            <el-option label="年" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="编码前缀" prop="preCode">
          <el-input-number v-model="form.preCode" :min="10" :max="99" placeholder="单位编码前缀" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" disabled :label="0">是</el-radio>
          <el-radio v-model="form.isDisable" disabled :label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus == 'create'?handleCreate():handleModify()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getBizType, saveBizType, delBizType, updateBizTypeDisabled } from '@/api/basedata'
export default {
  name: 'BaseBizType',
  data() {
    return {
      tableKey: 0,
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        bizTypeName: ''
      },
      form: {
        id: '',
        bizTypeName: '',
        bizTypeCode: '',
        len: '',
        dateType: '',
        preCode: '',
        remarks: '',
        isDisable: 0
      },
      dialogVisible: false,
      dialogStatus: '',
      rules: {
        bizTypeName: [{ required: true, message: '名称不能为空', trigger: 'change' }],
        bizTypeCode: [{ required: true, message: '代码不能为空', trigger: 'change' }],
        isDisable: [{ required: false }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getBizType(this.listQuery).then(response => {
        this.listLoading = false
        this.tableData = response.data.data
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      for (const key in this.form) {
        this.form[key] = obj[key]
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateStatus(data) {
      this.$confirm('确定' + (data.isDisable == 1 ? '解禁？' : '禁用？'), '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changeAvaiable(data)
      })
    },
    changeAvaiable(data) {
      var obj = { id: data.id, isDisable: data.isDisable == 1 ? 0 : 1 }
      updateBizTypeDisabled(obj).then(res => {
        if (res.data.errorCode == 0) {
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem(id)
      })
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveBizType(this.form).then(res => {
            if (res.data.errorCode == 0) {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleCreate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveBizType(this.form).then(res => {
            if (res.data.errorCode == 0) {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    delItem(id) {
      delBizType(id).then(res => {
        if (res.data.errorCode == 0) {
          this.getList()
          this.dialogVisible = false
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>
